package com.defysoft.waukids.main;

import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Main_ChangePass extends BaseActivity implements OnClickListener{
	Toolbar toolbar;
	TextView title;
	EditText editText[] = new EditText[3];
	Button button;
	String email, nowPass, newPass;
	Sqlite_Connect sqlite_Connect;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	private final int CHANGE_PASS = 1;
	private final int SUCCESS_CHANGE = 9;
	private final int FAIR_DATA = 10;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		//email = "testmember1";
		email = getIntent().getStringExtra("MEM_EMAIL");
		if (email == null || email == "") {
			Toast.makeText(this,"잘못된 접근입니다.", Toast.LENGTH_SHORT).show();
		}
		setContentView(R.layout.main_changepass_activity);
		setStartData();
		setToolbar();
	}

	@Override
	public void setStartData() {
		super.setStartData();
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();
		sqlite_Connect = new Sqlite_Connect(this);
		
		title =(TextView)findViewById(R.id.MAIN_CHANGEPASS_TOOLBAR_TEXTVIEW_TITLE);
		editText[0] = (EditText)findViewById(R.id.MAIN_CHANGEPASS_EDITTEXT_NOWPASS);
		editText[1] = (EditText)findViewById(R.id.MAIN_CHANGEPASS_EDITTEXT_NEWPASS_ONE);
		editText[2] = (EditText)findViewById(R.id.MAIN_CHANGEPASS_EDITTEXT_NEWPASS_TWO);
		button = (Button)findViewById(R.id.MAIN_CHANGEPASS_BUTTON_NEXT);
		button.setOnClickListener(this);
		//비밀번호2 동일 확인
		editText[2].addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				final String ss = s.toString();
				if (ss.equals(editText[1].getText().toString())) {
					Toast.makeText(Main_ChangePass.this, "새로운 비밀번호 확인이 일치합니다.", Toast.LENGTH_SHORT).show();
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,	int after) {			}
			@Override
			public void afterTextChanged(Editable s) {			}
		});
	}
	
	private void setToolbar() {
		toolbar = (Toolbar)findViewById(R.id.MAIN_CHANGEPASS_TOOLBAR);
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
					finish();
			}
		});
	}

	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);
	switch (_message.what) {
	case SUCCESS_CHANGE:
		endDIal();
		if (firstThread != null) {firstThread.interrupt();}
		Toast.makeText(this, (String)_message.obj, Toast.LENGTH_SHORT).show();
		startActivity(new Intent(Main_ChangePass.this, Main_Intro.class));
		finish();
		break;

	case FAIR_DATA:
		endDIal();
		if (firstThread != null) {firstThread.interrupt();}		
		Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT).show();
		break;
		
	case CATCH_EXCEPTION:
		endDIal();
		if (firstThread != null) {firstThread.interrupt();}		
		sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Main_ChangePass", member.Email);
		break;
		}
	}

	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case CHANGE_PASS:
					changePass();
					break;
				}
			}
		});
		startDIal();
		firstThread.start();
	}

	protected void changePass() {
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code="+ CODE_MAIN_CHANGEPASS_CHANGE +"&email="+ email +
					"&nowpass="+nowPass+"&newpass="+newPass));
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS_CHANGE, jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIR_DATA, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString()+" where changePass()");
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.MAIN_CHANGEPASS_BUTTON_NEXT:		//비밀번호 바꾸기 클릭.
			checkPass();	//입력 양식 확인
			break;
		default:
			break;
		}
	}
	
	private void checkPass() {
		int check = 0;
		nowPass = editText[0].getText().toString();	//기존 비밀번호.
		newPass = editText[1].getText().toString();	//새로운비번.
		
		if (editText[0].getText().toString().length() <= 0) {
			editText[0].setHint("현재 비밀번호를 입력해주세요.");
			editText[0].setHintTextColor(Color.RED);
			check = 1;
		} else if (editText[1].getText().toString().length() <= 0
				&& editText[2].getText().toString().length() <= 0) { // 새로운 비번이
			// 없을때.

editText[1].setHint("새 비밀번호를 입력해 주세요.");
editText[1].setHintTextColor(Color.RED);
showPopup("알림","새 비밀번호를 입력하세요.\n비밀번호 변경을 원하지 않을경우,\n기존 비밀번호를 입력해 주세요.",
0);
check = 1;

}
		
		
		
		
		else if(editText[1].getText().toString().length() > 0){	//새 비밀번호를 입력했을 경우에만.
			if (editText[1].getText().length()<8 || editText[1].getText().length() >15) {
				editText[1].setText("");
				editText[1].setHint("패스워드를 8자~15자로 입력하세요.");
				editText[1].setHintTextColor(Color.RED);
				check = 1;
			} else if (editText[2].getText().toString().length() < 0) {
				editText[2].setHint("새 비밀번호 확인을 입력해주세요.");
				editText[2].setHintTextColor(Color.RED);
				check = 1;
			} else if (!editText[2].getText().toString().equals(editText[1].getText().toString())) {
					editText[2].setText("");
					editText[2].setHint("새로운 비밀번호가 일치하지 않습니다.");
					editText[2].setHintTextColor(Color.RED);
					check = 1;
			}
		}
		if (check == 0) {
			startNetwork(CHANGE_PASS);
		}
	}
	
	
}
